package com.dormitory.myt.sushecontroller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dormitory.myt.common.R;
import com.dormitory.myt.en.Liusu;
import com.dormitory.myt.mapper.LiusuMapper;
import com.dormitory.myt.service.LiusuServcie;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/liusu")
@Slf4j
public class LiusuContorller {
@Autowired
private LiusuServcie liusuServcie;
@Autowired
private LiusuMapper liusuMapper;
    /**
     * 学生端留宿申请
     * @param response
     * @param sushehao
     * @param floor
     * @param content
     * @param reporttime
     * @return
     */
    @GetMapping("/up")
    public R<String> xsxs(HttpServletResponse response,@RequestParam Integer sushehao, @RequestParam String floor, @RequestParam String content,@RequestParam String reporttime){
        response.setHeader("Access-Control-Allow-Origin","*");
        Liusu liusu=new Liusu();
        liusu.setSushehao(sushehao);
        liusu.setFloor(floor);
        liusu.setContent(content);
        liusu.setReporttime(reporttime);
        liusuServcie.save(liusu);  //可能一个专业有多个数据的风险，bug！
        return R.success("year");
    }

    /**
     * 学生端状态查询
     * @param response
     * @param sushehao
     * @return
     */
    @GetMapping("/state")
    public R<Liusu> xsxs1(HttpServletResponse response,@RequestParam Integer sushehao){
        response.setHeader("Access-Control-Allow-Origin","*");

        LambdaQueryWrapper<Liusu> queryWrapper = new LambdaQueryWrapper<>();
        //根据宿舍来查询：
        queryWrapper.eq(Liusu::getSushehao,sushehao);
        List<Liusu> list = liusuServcie.list(queryWrapper);
        Liusu l = list.get(0);
        return R.success(l);
    }

    /**
     * 宿管端查询所有申请
     * @param response
     * @return
     */
    @GetMapping("/select")
    public R<List<Liusu>> getLiusu(HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");

        LambdaQueryWrapper<Liusu> queryWrapper = new LambdaQueryWrapper<>();

        List<Liusu> list=liusuServcie.list(queryWrapper);

        return R.success(list);

    }

    /**
     * 宿管点击处理
     * @param response
     * @param sushehao
     * @param state
     * @param clicktime
     * @return
     */
    @GetMapping
    public R<String> getLiusu2(HttpServletResponse response, @RequestParam int sushehao,@RequestParam int state, @RequestParam String clicktime){
        response.setHeader("Access-Control-Allow-Origin","*");

        Liusu l = new Liusu();
        l.setSushehao(sushehao);
        l.setState(state);
        l.setClicktime(clicktime);
        LambdaUpdateWrapper<Liusu> updateWrapper = Wrappers.<Liusu>lambdaUpdate()
                .eq(Liusu::getSushehao, l.getSushehao());
        liusuServcie.update(l, updateWrapper);


        return R.success("year");
    }


}
